package com.atguigu03.date.jdk8;

import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class DateTimeTest {


    @Test
    public void test4() {

        LocalDate localDate = LocalDate.now();
        LocalTime now = LocalTime.now();
        LocalDateTime now1 = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(now);
        System.out.println(now1);
        System.out.println("******************");


        LocalDate of = LocalDate.of(2022, 5, 20);
        LocalTime of1 = LocalTime.of(15, 8, 22);
        LocalDateTime of2 = LocalDateTime.of(2022, 5, 12, 11, 10);

        System.out.println(of);
        System.out.println(of1);
        System.out.println(of2);

        System.out.println("******************");

        LocalDate localDate1 = of.withDayOfMonth(22);
        LocalTime localTime = of1.withHour(16);

        System.out.println(localDate1);
        System.out.println(localTime);


    }

    @Test
    public void test5() {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");

        LocalDateTime now = LocalDateTime.now();

        //格式化
        String format = dateTimeFormatter.format(now);
        System.out.println(format);

        //解析
        TemporalAccessor parse = dateTimeFormatter.parse("2023-05-21 15-29-24");
        System.out.println(parse.getClass());//class java.time.format.Parsed
        LocalDateTime from = LocalDateTime.from(parse);
        System.out.println(from);



    }

}
